package hotLeetcode;

public class Test3 {


    public static <T extends Comparable<T>> void sort(T[] array) {
        int n = array.length;
        boolean swap;
        for (int i = 0; i < n - 1; i++) {
            swap = false;
            for (int j = 0; j < n - 1 - i; j++) {
                if (array[j].compareTo(array[j + 1]) > 0) {
                    T temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                    swap = true;
                }
            }
            if (!swap) break;
        }
    }



    public static void main(String[] args) {
        // 测试Integer数组排序
        Integer[] intArray = {5, 3, 8, 1, 2};
        sort(intArray);
        System.out.println("Sorted Integer array: " + java.util.Arrays.toString(intArray));

        // 测试String数组排序
        String[] stringArray = {"banana", "apple", "cherry"};
        sort(stringArray);
        System.out.println("Sorted test array: " + java.util.Arrays.toString(stringArray));

        // 测试Long数组排序
        Long[] longArray = {5L, 3L, 8L, 1L, 2L};
        sort(longArray);
        System.out.println("Sorted Long array: " + java.util.Arrays.toString(longArray));
    }
}
